﻿package com.shangda.util;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

import com.shangda.entity.ReceiveData;

public class DataConvert {
	
	//转成16进制
	public static String byte2hex(short[] buffer,int length){
		String h = "";
		for (int i=0;i<length; i++){
			String temp = Integer.toHexString(buffer[i]&0xFF);
			if (temp.length() == 1){
				temp = "0" + temp;
			}
			h = h + " " + temp;
		}
		return h;
	}		
	
	
	public static double binaryExp(int exp)
	{
	    if (exp == 0) {
	        return 1;
	    } 
	    
	    double result = 1.0;
	    if (exp > 0) {
	        for (int i = 0; i < exp; ++i) {
	            result *= 2;
	        }
	    } else {
	        for (int i = 0; i < -exp; ++i) {
	            result *= 0.5;
	        }
	    }
	    
	    return result;
	}
	
	//解析double类型的方法
	/*public static double unsingedcharArray2double(short dataArray[])
	{
	    short valueLength = 8;
	    
	    // 将数组倒序， [0] ~ [7]:最高位 --> 最低位
	    for (int i = 0, j = valueLength - 1; i<j; ++i, --j) {
	        short tmp = dataArray[i];
	        dataArray[i] = dataArray[j];
	        dataArray[j] = tmp;
	    }
	    
	    // 确定符号位
	    short signValue = (short) ((dataArray[0] >> 7) & (0x01)); // 符号位:0为正数, 1为负数
	    
	    // 确定指数部分(double类型共11位)
	    int expValue = 0;
	    short data_0 = dataArray[0];
	    short data_1 = dataArray[1];
	    expValue = data_0 & 0x7f;  // 最高位的7位
	    expValue = expValue << 4;
	    expValue += (data_1 >> 4) & (0x0f); // 次高位的4位，共11位
	    
	    // 确定尾数部分(double类型共52位)
	    long manValue = 0;   // long占8个字节
	    manValue = data_1 & 0x0f;    // 取低4位
	    for (int i = 2; i < valueLength; ++i) {
	        manValue = manValue << 8;
	        manValue += dataArray[i];
	    }
	    
	    // 根据符号位，指数部分和尾数部分 来计算double值
	    int expCount = expValue - 1023;    // double类型是1023
	    double d = binaryExp(expCount);
	    expCount--;
	    for (int i = 51; i >= 0; --i) {
	       short  sign = (short) ((manValue >> i) & (0x01));
	        d += sign * binaryExp(expCount--);
	    }
	    
	    // 判断是否为负数
	    if (signValue == 1) {
	        d = -d;
	    }
	    
	    if (d - 0.0 < 0.000001 && d - 0.0 > -0.000001){
	    	return 0;
	    }
	    
	    
	    return d;
	}*/
	
	
	
	//T、PH、ORP、DO等的解析
	private static double VOL_MAX = 3.3;
	private static double VAL_MAX = 1024.0;
	private static double REG_MIN = 0.6;
	private static double REG_MAX = 3.0;
	
	private static double T_MAX  = 50.0;
	private static double T_MIN  = 0.0;

	private static double PH_MAX = 12.0;
	private static double PH_MIN = 2.0;

	private static double ORP_MAX = 1999.0;
	private static double ORP_MIN = -1999.0;

	private static double DO_MAX  = 20.0;
	private static double DO_MIN  = 0.0;


	private static double LS_MAX = 15.0;
	private static double LS_MIN = 0.0;


	private static double FLOW_MAX = 15.0;
	private static double FLOW_MIN = 0.0;

	private static double GET_MAX = (1024.0 / 3.3 * 3.0);
	private static double GET_MIN = (1024.0 / 3.3 * 0.6);

	public static double ConvertV2T(int v)
	{
		return ((double)v - GET_MIN) * (T_MAX - T_MIN) / (GET_MAX - GET_MIN) + T_MIN; 
	}



	public static double ConvertV2PH(int v)
	{
		return ((double)v - GET_MIN) * (PH_MAX - PH_MIN) / (GET_MAX - GET_MIN) + PH_MIN;

	}

	public static double ConvertV2ORP(int v)
	{
		return ((double)v - GET_MIN) * (ORP_MAX - ORP_MIN) / (GET_MAX - GET_MIN) + ORP_MIN;

	}

	public static double ConvertV2DO(int v)
	{
		return ((double)v - GET_MIN) * (DO_MAX - DO_MIN) / (GET_MAX - GET_MIN) + DO_MIN;

	}
	public static double ConvertV2FLOW(int v)
	{
		return ((double)v - GET_MIN) * (FLOW_MAX - FLOW_MIN) / (GET_MAX - GET_MIN) + FLOW_MIN;

	}
	
	
	//数据包格式解析函数  江都数据解析
	public static ReceiveData getReceiveData76(short [] datas) throws IOException{
		
		// 开始解析
		// 判断数组长度
		if (datas.length != 76)
			return null;

		// 判断开头和结尾
		if (!((datas[0] == 0x23 && datas[1] == 0x23) && (datas[datas.length - 2] == 0x0d && datas[datas.length - 1] == 0x0a))) {
			return null;
		}
		
		ReceiveData receiveData = new ReceiveData();
		
		//表示当前指针位置
		int temp = 0;
		int pos = 2;
		String time;
		for (int i = 0; i < 4; ++i){
			temp = temp * 10 + (datas[pos++] - '0');
		}
		time = temp+"/";
		temp = 0;
		for (int i = 0; i < 2; ++i){
			temp = temp * 10 + (datas[pos++] - '0');
		}
		time = time + temp +"/";
		temp = 0;
		for (int i = 0; i < 2; ++i){
			temp = temp * 10 + (datas[pos++] - '0');
		}
		time = time + temp +" ";
		temp = 0;
		for (int i = 0; i < 2; ++i){
			temp = temp * 10 + (datas[pos++] - '0'); 
		}
		time = time + temp +":";
		temp = 0;
		for (int i = 0; i < 2; ++i){
			temp = temp * 10 + (datas[pos++] - '0'); 
		}
		time = time + temp +":";
		temp = 0;
		for (int i = 0; i < 2; ++i){
			temp = temp * 10 + (datas[pos++] - '0'); 
		}
		time = time + temp;
		receiveData.setDate(time);
		
		// 解析分包长度
		//pos = 16;
		temp = 0;
		for (int i = 0; i < 4; i++) {
			temp = temp * 10 + datas[pos++] - '0';
		}
		
		// 解析站点编号
		// 此时pos为20
		temp = datas[pos++] * 256 + datas[pos++];
		receiveData.setSewageId(temp);
		
		// 解析温度
		temp = pos + 3;
		temp = convert(datas, temp);
		double T = DataConvert.ConvertV2T(temp);
		T = (double)Math.round(T*100)/100;  
		receiveData.setT(T);
		pos += 4;
		
		
		//解析PH
		temp = pos + 3;
		temp = convert(datas,temp);
		double ph = DataConvert.ConvertV2PH(temp);
		ph = (double)Math.round(ph*100)/100;  
		receiveData.setPh(ph);
	    pos += 4;
		
		//解析ORP
		temp = pos + 3;
		temp = convert(datas,temp);
		double orp = DataConvert.ConvertV2ORP(temp);
		orp = (double)Math.round(orp*100)/100;  
		receiveData.setOrp(orp);
		pos += 4;
		
		//解析DO
	    temp = pos + 3;
	    temp = convert(datas, temp);
	    double Do = DataConvert.ConvertV2DO(temp);
	    Do = (double)Math.round(Do*100)/100;  
	    receiveData.setDo(Do);
	    pos += 4;
	    
	    //解析flow
	    temp = pos + 3;
	    temp = convert(datas,temp);
	    double flow = DataConvert.ConvertV2FLOW(temp);
	    flow = (double)Math.round(flow*100)/100;  
	    receiveData.setFlow(flow);
	    
	    
	  //解析液位及原设备的pump信息
	    pos += 4;
	    temp = datas[pos++];
	    receiveData.setLs1(temp);
	
		temp = datas[pos++];
		receiveData.setPumpStatus1(temp);
		
		temp = datas[pos++];
		receiveData.setPumpStatus2(temp);
		
		temp = datas[pos++];
		receiveData.setPumpStatus3(temp);
		
		temp = datas[pos++];
		receiveData.setPumpStatus4(temp);
		
		temp = datas[pos++];
		receiveData.setPumpStatus5(temp);
		
		temp = datas[pos++];
		receiveData.setPumpStatus6(temp);
		
		temp = datas[pos++];
		receiveData.setLs2(temp);
		
		// 解析瞬时流量和总流量
		byte[] flows = new byte[8];
		DataInputStream dis = null;
		for (int i = 7; i >=0; i--) {
			flows[i] = (byte) datas[pos++];
		}
		dis = new DataInputStream(new ByteArrayInputStream(flows));
		double actualFlow = dis.readDouble();
		actualFlow = (double)Math.round(actualFlow*100)/100;;
		receiveData.setActuralFlow(actualFlow);
		dis.close();
		
		for (int i = 7; i >=0 ; i--) {
			flows[i] = (byte) datas[pos++];
		}
		dis = new DataInputStream(new ByteArrayInputStream(flows));
		double sumFlow = dis.readDouble();
		sumFlow = (double)Math.round(sumFlow*100)/100;
		receiveData.setSumFlow(sumFlow);
		dis.close();
		// 解析提升池液位
		int elevate = 0;
		int bit = getBit(datas[pos], 1);
		if (bit == 1) {
			elevate = 1;
		} else {
			bit = getBit(datas[pos], 2);
			if (bit == 1) {
				elevate = 2;
			} else {
				bit = getBit(datas[pos], 4);
				if (bit == 1) {
					elevate = 8;
				} else {
					bit = getBit(datas[pos], 3);
					if (bit == 1) {
						elevate = 4;
					}
				}
			}
		}
		receiveData.setElevateLs(elevate);
		
		
		// 解析原水池
		pos++;
		int waterLs = 0;
		bit = getBit(datas[pos], 1);
		if (bit == 1) {
			waterLs = 1;
		} else {
			bit = getBit(datas[pos], 2);
			if (bit == 1) {
				waterLs = 2;
			} else {
				bit = getBit(datas[pos], 4);
				if (bit == 1) {
					waterLs = 8;
				} else {
					bit = getBit(datas[pos], 3);
					if (bit == 1) {
						waterLs = 4;
					}
				}
			}
		}
		receiveData.setWaterLs(waterLs);
		
		
		// 解析各设备状态
		pos++;
		bit = getBit(datas[pos], 1);
		int[] pumps = new int[15];
		// 提升泵1
		if (bit == 1) {
			pumps[0] = 1;
		} else {
			bit = getBit(datas[pos + 2], 1);
			if (bit == 1) {
				pumps[0] = 3;
			} else {
				pumps[0] = 2;
			}
		}
		
		
		//提升泵2
		bit = getBit(datas[pos], 2);
		if (bit == 1){
			pumps[1] = 1;
		}else{
			bit = getBit(datas[pos+2], 2);
			if (bit == 1){
				pumps[1] = 3;
			}else{
				pumps[1] = 2;
			}
		}
		
		// 原水泵1
		bit = getBit(datas[pos], 3);
		if (bit == 1) {
			pumps[2] = 1;
		} else {
			bit = getBit(datas[pos + 2], 3);
			if (bit == 1) {
				pumps[2] = 3;
			} else {
				pumps[2] = 2;
			}
		}
		
		
		
		// 原水泵2
		bit = getBit(datas[pos], 4);
		if (bit == 1) {
			pumps[3] = 1;
		} else {
			bit = getBit(datas[pos + 2], 4);
			if (bit == 1) {
				pumps[3] = 3;
			} else {
				pumps[3] = 2;
			}
		}
		
		

		// 风机1
		bit = getBit(datas[pos], 5);
		if (bit == 1) {
			pumps[4] = 1;
		} else {
			bit = getBit(datas[pos + 2], 5);
			if (bit == 1) {
				pumps[4] = 3;
			} else {
				pumps[4] = 2;
			}
		}
		

		// 风机2
		bit = getBit(datas[pos], 6);
		if (bit == 1) {
			pumps[5] = 1;
		} else {
			bit = getBit(datas[pos + 2], 6);
			if (bit == 1) {
				pumps[5] = 3;
			} else {
				pumps[5] = 2;
			}
		}
		
		
		
		// 1#混合液回流泵
		bit = getBit(datas[pos], 7);
		if (bit == 1) {
			pumps[6] = 1;
		} else {
			bit = getBit(datas[pos + 2], 7);
			if (bit == 1) {
				pumps[6] = 3;
			} else {
				pumps[6] = 2;
			}
		}
		
		// 2#混合液回流泵
		bit = getBit(datas[pos], 8);
		if (bit == 1) {
			pumps[7] = 1;
		} else {
			bit = getBit(datas[pos + 2], 8);
			if (bit == 1) {
				pumps[7] = 3;
			} else {
				pumps[7] = 2;
			}
		}
		
		pos ++;
		
		//污泥回流泵1
		bit = getBit(datas[pos], 1);
		if (bit == 1) {
			pumps[8] = 1;
		} else {
			bit = getBit(datas[pos + 2], 1);
			if (bit == 1) {
				pumps[8] = 3;
			} else {
				pumps[8] = 2;
			}
		}
		
		//污泥回流泵2
		bit = getBit(datas[pos], 2);
		if (bit == 1) {
			pumps[9] = 1;
		} else {
			bit = getBit(datas[pos + 2], 2);
			if (bit == 1) {
				pumps[9] = 3;
			} else {
				pumps[9] = 2;
			}
		}
		
		
		//消毒机
		bit = getBit(datas[pos], 3);
		if (bit == 1) {
			pumps[10] = 1;
		} else {
			bit = getBit(datas[pos + 2], 3);
			if (bit == 1) {
				pumps[10] = 3;
			} else {
				pumps[10] = 2;
			}
		}
		
		
		//电磁阀1
		bit = getBit(datas[pos], 4);
		if (bit == 1){
			pumps[11] = 1;
		}else{
			pumps[11] = 2;
		}
		
		//电磁阀2
		bit = getBit(datas[pos], 5);
		if (bit == 1){
			pumps[12] = 1;
		}else{
			pumps[12] = 2;
		}
		//电磁阀3
		bit = getBit(datas[pos], 6);
		if (bit == 1){
			pumps[13] = 1;
		}else{
			pumps[13] = 2;
		}
		//电磁阀4
		bit = getBit(datas[pos], 7);
		if (bit == 1){
			pumps[14] = 1;
		}else{
			pumps[14] = 2;
		}
		//解析完毕================
		receiveData.setPumpStatus(pumps);
		
		
		
		return receiveData;
	}
	
	
	//端口8990的数据处理
	public static ReceiveData getReceiveData70(short [] datas) throws IOException{
		
		// 开始解析
		// 判断数组长度
		if (datas.length != 70)
			return null;

		// 判断开头和结尾
		if (!((datas[0] == 0x23 && datas[1] == 0x23) && (datas[datas.length - 2] == 0x0d && datas[datas.length - 1] == 0x0a))) {
			return null;
		}
		
		ReceiveData receiveData = new ReceiveData();
		
		//表示当前指针位置
		int temp = 0;
		int pos = 2;
		String time;
		for (int i = 0; i < 4; ++i){
			temp = temp * 10 + (datas[pos++] - '0');
		}
		time = temp+"/";
		temp = 0;
		for (int i = 0; i < 2; ++i){
			temp = temp * 10 + (datas[pos++] - '0');
		}
		time = time + temp +"/";
		temp = 0;
		for (int i = 0; i < 2; ++i){
			temp = temp * 10 + (datas[pos++] - '0');
		}
		time = time + temp +" ";
		temp = 0;
		for (int i = 0; i < 2; ++i){
			temp = temp * 10 + (datas[pos++] - '0'); 
		}
		time = time + temp +":";
		temp = 0;
		for (int i = 0; i < 2; ++i){
			temp = temp * 10 + (datas[pos++] - '0'); 
		}
		time = time + temp +":";
		temp = 0;
		for (int i = 0; i < 2; ++i){
			temp = temp * 10 + (datas[pos++] - '0'); 
		}
		time = time + temp;
		receiveData.setDate(time);
		
		// 解析分包长度
		//pos = 16;
		temp = 0;
		for (int i = 0; i < 4; i++) {
			temp = temp * 10 + datas[pos++] - '0';
		}
		
		// 解析站点编号
		// 此时pos为20
		temp = datas[pos++] * 256 + datas[pos++];
		
		receiveData.setSewageId(temp);
		
		// 解析温度
		temp = pos + 3;
		temp = convert(datas, temp);
		double T = DataConvert.ConvertV2T(temp);
		receiveData.setT(T);
		pos += 4;
		
		
		//解析PH
		temp = pos + 3;
		temp = convert(datas,temp);
		double ph = DataConvert.ConvertV2PH(temp);
		receiveData.setPh(ph);
	    pos += 4;
		
		//解析ORP
		temp = pos + 3;
		temp = convert(datas,temp);
		double orp = DataConvert.ConvertV2ORP(temp);
		receiveData.setOrp(orp);
		pos += 4;
		
		//解析DO
	    temp = pos + 3;
	    temp = convert(datas, temp);
	    double Do = DataConvert.ConvertV2DO(temp);
	    receiveData.setDo(Do);
	    pos += 4;
	    
	    //解析flow
	    temp = pos + 3;
	    temp = convert(datas,temp);
	    double flow = DataConvert.ConvertV2FLOW(temp);
	    receiveData.setFlow(flow);
	    
	    
	  //解析液位及原设备的pump信息
	    pos += 4;
	    temp = datas[pos++];
	    receiveData.setLs1(temp);
	
		temp = datas[pos++];
		if (temp == 1){
			temp = 2;
		}else if (temp == 2){
			temp = 1;
		}
		receiveData.setPumpStatus1(temp);
		
		temp = datas[pos++];
		if (temp == 1){
			temp = 2;
		}else if (temp == 2){
			temp = 1;
		}
		receiveData.setPumpStatus2(temp);
		
		temp = datas[pos++];
		if (temp == 1){
			temp = 2;
		}else if (temp == 2){
			temp = 1;
		}
		receiveData.setPumpStatus3(temp);
		
		temp = datas[pos++];
		if (temp == 1){
			temp = 2;
		}else if (temp == 2){
			temp = 1;
		}
		receiveData.setPumpStatus4(temp);
		
		temp = datas[pos++];
		if (temp == 1){
			temp = 2;
		}else if (temp == 2){
			temp = 1;
		}
		receiveData.setPumpStatus5(temp);
		
		temp = datas[pos++];
		if (temp == 1){
			temp = 2;
		}else if (temp == 2){
			temp = 1;
		}
		receiveData.setPumpStatus6(temp);
		
		temp = datas[pos++];
		receiveData.setLs2(temp);
		
		// 解析瞬时流量和总流量
		byte[] flows = new byte[8];
		DataInputStream dis = null;
		for (int i = 7; i >= 0; i--) {
			flows[i] = (byte) datas[pos++];
		}
		dis = new DataInputStream(new ByteArrayInputStream(flows));
		double actualFlow =  dis.readDouble();
		receiveData.setActuralFlow(actualFlow);
		dis.close();
		
		for (int i=7 ; i>=0; i--) {
			flows[i] = (byte) datas[pos++];
		}
		dis = new DataInputStream(new ByteArrayInputStream(flows));
		double sumFlow = dis.readDouble();
		receiveData.setSumFlow(sumFlow);
		dis.close();
		return receiveData;
	}
	
	
	//端口9990的数据处理
	public static ReceiveData getReceiveData106(short[] datas) throws Exception {

		// 开始解析
		// 判断数组长度
		if (datas.length != 106)
			return null;

		// 判断开头和结尾
		if (!((datas[0] == 0x23 && datas[1] == 0x23) && (datas[datas.length - 2] == 0x0d && datas[datas.length - 1] == 0x0a))) {
			return null;
		}

		ReceiveData receiveData = new ReceiveData();

		// 表示当前指针位置
		int temp = 0;
		int pos = 2;
		String time;
		for (int i = 0; i < 4; ++i) {
			temp = temp * 10 + (datas[pos++] - '0');
		}
		time = temp + "/";
		temp = 0;
		for (int i = 0; i < 2; ++i) {
			temp = temp * 10 + (datas[pos++] - '0');
		}
		time = time + temp + "/";
		temp = 0;
		for (int i = 0; i < 2; ++i) {
			temp = temp * 10 + (datas[pos++] - '0');
		}
		time = time + temp + " ";
		temp = 0;
		for (int i = 0; i < 2; ++i) {
			temp = temp * 10 + (datas[pos++] - '0');
		}
		time = time + temp + ":";
		temp = 0;
		for (int i = 0; i < 2; ++i) {
			temp = temp * 10 + (datas[pos++] - '0');
		}
		time = time + temp + ":";
		temp = 0;
		for (int i = 0; i < 2; ++i) {
			temp = temp * 10 + (datas[pos++] - '0');
		}
		time = time + temp;
		receiveData.setDate(time);

		// 解析分包长度
		// pos = 16;
		temp = 0;
		for (int i = 0; i < 4; i++) {
			temp = temp * 10 + datas[pos++] - '0';
		}

		// 解析站点编号
		// 此时pos为20
		temp = datas[pos++] * 256 + datas[pos++];

		receiveData.setSewageId(temp);

		// 解析温度
		temp = pos + 3;
		temp = convert(datas, temp);
		double T = DataConvert.ConvertV2T(temp);
		receiveData.setT(T);
		pos += 4;

		// 解析PH
		temp = pos + 3;
		temp = convert(datas, temp);
		double ph = DataConvert.ConvertV2PH(temp);
		receiveData.setPh(ph);
		pos += 4;

		// 解析ORP
		temp = pos + 3;
		temp = convert(datas, temp);
		double orp = DataConvert.ConvertV2ORP(temp);
		receiveData.setOrp(orp);
		pos += 4;

		// 解析DO
		temp = pos + 3;
		temp = convert(datas, temp);
		double Do = DataConvert.ConvertV2DO(temp);
		receiveData.setDo(Do);
		pos += 4;

		// 解析flow
		temp = pos + 3;
		temp = convert(datas, temp);
		double flow = DataConvert.ConvertV2FLOW(temp);
		receiveData.setFlow(flow);

		// 解析液位及原设备的pump信息
		pos += 4;
		temp = datas[pos++];
		receiveData.setLs1(temp);

		temp = datas[pos++];
		receiveData.setPumpStatus1(temp);

		temp = datas[pos++];
		receiveData.setPumpStatus2(temp);

		temp = datas[pos++];
		receiveData.setPumpStatus3(temp);

		temp = datas[pos++];
		receiveData.setPumpStatus4(temp);

		temp = datas[pos++];
		receiveData.setPumpStatus5(temp);

		temp = datas[pos++];
		receiveData.setPumpStatus6(temp);

		temp = datas[pos++];
		receiveData.setLs2(temp);

		// 解析瞬时流量和总流量
		byte[] flows = new byte[8];
		DataInputStream dis = null;
		for (int i = 7; i >=0; i--) {
			flows[i] = (byte) datas[pos++];
		}
		dis= new DataInputStream(new ByteArrayInputStream(flows));
		double actualFlow = dis.readDouble();
		receiveData.setActuralFlow(actualFlow);
		dis.close();
		
		
		for (int i = 7; i >= 0; i--) {
			flows[i] = (byte) datas[pos++];
		}
		dis= new DataInputStream(new ByteArrayInputStream(flows));
		double sumFlow = dis.readDouble();
		receiveData.setSumFlow(sumFlow);
		dis.close();
	
		//获取cod
		byte [] temp2 = new byte[4];
		float [] params = new float [4];
		for (int i=0;i<4;i++){
			temp2[i] = (byte) datas[pos++];
		}
		byte byteTemp = 0;
		byte byteTemp2 = 0;
		byteTemp = temp2[0];
		byteTemp2 = temp2[1];
		temp2[0] = temp2[2];
		temp2[1] = temp2[3];
		temp2[2] = byteTemp;
		temp2[3] = byteTemp2;
		dis= new DataInputStream(new ByteArrayInputStream(temp2));
		params[0] = dis.readFloat();
		dis.close();
		
		
		//获取AN
		for (int i=0;i<4;i++){
			temp2[i] = (byte) datas[pos++];
		}
		byteTemp = temp2[0];
		byteTemp2 = temp2[1];
		temp2[0] = temp2[2];
		temp2[1] = temp2[3];
		temp2[2] = byteTemp;
		temp2[3] = byteTemp2;
		dis = new DataInputStream(new ByteArrayInputStream(temp2));
		params[1] = dis.readFloat();
		dis.close();
		
		//获取TP
		for (int i=0;i<4;i++){
			temp2[i] = (byte) datas[pos++];
		}
		byteTemp = temp2[0];
		byteTemp2 = temp2[1];
		temp2[0] = temp2[2];
		temp2[1] = temp2[3];
		temp2[2] = byteTemp;
		temp2[3] = byteTemp2;
		dis = new DataInputStream(new ByteArrayInputStream(temp2));
		params[2] = dis.readFloat();
		dis.close();
		
		//获取浊度
		for (int i=0;i<4;i++){
			temp2[i] = (byte) datas[pos++];
		}
		dis = new DataInputStream(new ByteArrayInputStream(temp2));
		params[3] = dis.readFloat();
		dis.close();
		
		receiveData.setParams(params);
		
		//获取瞬时流量
		for(int i=0;i<4;i++){
			temp2[i] = (byte)datas[pos++];
		} 
		dis = new DataInputStream(new ByteArrayInputStream(temp2));
		receiveData.setActuralFlow(dis.readFloat());
		dis.close();
		
		//获取累积流量
		for (int i=0;i<4;i++){
			temp2[i] = (byte)datas[pos++];
		}
		dis = new DataInputStream(new ByteArrayInputStream(temp2));
		float tempData = dis.readInt();
		
		for (int i=0;i<4;i++){
			temp2[i] = (byte)datas[pos++];
		}
		dis = new DataInputStream(new ByteArrayInputStream(temp2));
		tempData = tempData + dis.readFloat();
		receiveData.setSumFlow(tempData);
		
		//获取电流电压
		for (int i=3;i>=0;i--){
			temp2[i] = (byte) datas[pos++];
		}
		dis = new DataInputStream(new ByteArrayInputStream(temp2));
		receiveData.setEleU(dis.readFloat());
		
		//获取电流电压
		for (int i=3;i>=0;i--){
			temp2[i] = (byte) datas[pos++];
		}
		dis = new DataInputStream(new ByteArrayInputStream(temp2));
		receiveData.setEleA(dis.readFloat());
		
		
		return receiveData;
	}
	
	
	
	//4个字节之间的转换
	public static int convert(short[] buffer, int length) {
		String h = "";
		for (int i=0;i<4; i++){
			String temp = Integer.toHexString(buffer[length]&0xFF);
			if (temp.length() == 1){
				temp = "0" + temp;
			}
			h = h + temp;
			length --;
		}
		return Integer.parseInt(h, 16);


	}
		
		
		//取位数的函数
		public static int getBit(short num,int index)
		{
			return (num>>(index-1)) & 0x00000001;
		}
	
}
